package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Autowired
    private BaseAttributeValueMapper baseAttributeValueMapper;

    @Override
    public List<BaseCategory1> findAllCategory1() {
        //  返回数据集合
        return baseCategory1Mapper.selectList(null);
    }

    @Override
    public JSONObject getBaseCategory(Long category1Id) {
        //  创建一个对象
        JSONObject category1 = new JSONObject();
        //  组装数据
        //  先获取一级分类对象
        //  BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);

        //  获取二级分类数据： select * from base_category_view where category1_id = 1;
        //  根据一级分类Id 查询的数据集合.
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
        //  key = 二级分类Id value = 二级对应的集合数据
        Map<Long, List<BaseCategoryView>> map2 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map2.entrySet().iterator();
        //  声明一个二级分类对象集合
        ArrayList<JSONObject> categoryChild2 = new ArrayList<>();
        //  循环遍历
        while (iterator.hasNext()){
            //  获取到entry对象
            Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
            //  获取二级分类Id
            Long category2Id = entry.getKey();
            //  获取二级分类集合数据
            List<BaseCategoryView> categoryViewList = entry.getValue();
            //  声明二级分类对象
            JSONObject category2 = new JSONObject();
            category2.put("categoryId",category2Id);
            category2.put("categoryName",categoryViewList.get(0).getCategory2Name());
            //  获取三级分类集合数据并添加到二级分类中.
            List<JSONObject> category3List = categoryViewList.stream().map(baseCategoryView -> {
                //  声明三级分类对象
                JSONObject category3 = new JSONObject();
                //  赋值
                category3.put("categoryId", baseCategoryView.getCategory3Id());
                category3.put("categoryName", baseCategoryView.getCategory3Name());
                return category3;
            }).collect(Collectors.toList());
            //  将三级分类数据添加到二级对象中.
            category2.put("categoryChild",category3List);
            //  添加二级分类对象到集合中
            categoryChild2.add(category2);
        }
        //  赋值：
        category1.put("categoryId",baseCategoryViewList.get(0).getCategory1Id());
        category1.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
        category1.put("categoryChild",categoryChild2);
        //  返回数据
        return category1;
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        /*
		select id from base_category2 where category1_id = 1;
		select * from base_category3 where category2_id in (101,102,103) limit 7;
		 */
        //	获取二级分类数据 -- 留给你们写sql
        LambdaQueryWrapper<BaseCategory2> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseCategory2::getCategory1Id, category1Id);
        wrapper.select(BaseCategory2::getId);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(wrapper);

        //	获取三级分类数据
        //	获取到二级分类Id 集合
        List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
        LambdaQueryWrapper<BaseCategory3> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.in(BaseCategory3::getCategory2Id, category2IdList).eq(BaseCategory3::getIsTop, 1).last(" limit 7 ");
        List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(wrapper3);
        //  返回数据
        return baseCategory3List;
    }

    @Override
    @GuiGuCache(prefix = "categoryView:")
    public BaseCategoryView getCategoryView(Long category3Id) {
		/*
		# id = category3_id
		select * from base_category_view where id =?;
		 */
        return baseCategoryViewMapper.selectById(category3Id);
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        //	调用mapper 层
        return baseAttributeMapper.selectAttributeByCategory1Id(category1Id);
    }

    //	shift+f2:快速定位错误行号 ctrl+i
    @Override
    public List<JSONObject> getBaseCategoryList() {
        //	声明一个集合来存储分类数据
        List<JSONObject> list = new ArrayList<>();

        //	1.	先查询所有的分类数据： select * from base_category_view
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
        //	2.  获取一级分类数据：去重！ 关键词 - 分组   R apply(T t); T泛型谁的泛型?这个集合的！BaseCategoryView
        //	key = category1Id value = List<BaseCategoryView>
        //	Map<Long, List<BaseCategoryView>> collect = baseCategoryViewList.stream().collect(Collectors.groupingBy(baseCategoryView -> baseCategoryView.getCategory1Id()));
        //	方法引用：
        Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //	获取一级分类Id ，一级分类名称.
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
        //	循环遍历 最外层循环15次！
        while (iterator.hasNext()) {
            //	获取数据
            Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
            //	构建一级分类对象
            JSONObject category1 = new JSONObject();
            //	获取一级分类Id
            Long category1Id = entry.getKey();
            //	获取一级分类名称
            List<BaseCategoryView> categoryViewList = entry.getValue();
            //	赋值：
            category1.put("categoryId", category1Id);
            category1.put("categoryName", categoryViewList.get(0).getCategory1Name());
            //	要获取二级分类数据：key = category2Id value = List<BaseCategoryView>
            Map<Long, List<BaseCategoryView>> map1 = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //	循环遍历 3次循环！
            //	声明一个集合来存储一级分类Id 对应的二级分类集合数据。
            List<JSONObject> categoryChild2 = new ArrayList<>();
            Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
            while (iterator1.hasNext()) {
                //	获取数据
                Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
                //	声明一个二级分类对象
                JSONObject category2 = new JSONObject();
                //	获取二级分类Id
                Long category2Id = entry1.getKey();
                List<BaseCategoryView> categoryViewList1 = entry1.getValue();
                //	赋值数据
                category2.put("categoryId", category2Id);
                category2.put("categoryName", categoryViewList1.get(0).getCategory2Name());
                //	获取三级分类集合数据 void accept(T t); 循环6次
                //	声明一个集合来存储三级分类数据
                //				List<JSONObject> categoryChild3 = new ArrayList<>();
                //				categoryViewList1.stream().forEach(baseCategoryView -> {
                //					//	声明一个三级分类对象
                //					JSONObject category3 = new JSONObject();
                //					category3.put("categoryId",baseCategoryView.getCategory3Id());
                //					category3.put("categoryName",baseCategoryView.getCategory3Name());
                //					categoryChild3.add(category3);
                //				});
                //	stream():
                List<JSONObject> categoryChild3 = categoryViewList1.stream().map(baseCategoryView -> {
                    //	声明一个三级分类对象
                    JSONObject category3 = new JSONObject();
                    category3.put("categoryId", baseCategoryView.getCategory3Id());
                    category3.put("categoryName", baseCategoryView.getCategory3Name());
                    //	返回对象
                    return category3;
                }).collect(Collectors.toList());
                //	存储三级分类数据
                category2.put("categoryChild", categoryChild3);
                //	添加到二级分类集合中
                categoryChild2.add(category2);
            }
            //	添加到一级分类中.
            category1.put("categoryChild", categoryChild2);
            list.add(category1);
        }
        //	返回集合数据
        return list;
    }
}
